/**
 * created by 江心才子, 2020/3/24
 * Copyright (c) 2020, 270628297@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.wyl.bluetoothapp

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.wyl.bluetoothapp.util.BlueToothHelper
import com.wyl.bluetoothapp.util.BlueToothHelperCallback

/**
 * 项目名称：BluetoothApp
 * 创建人：江心才子
 * 创建时间：2020-03-24 09:37
 * 内容描述：Android蓝牙开发流程实践
开启 扫描 配对 连接 通信
 * 修改说明：
 */
const val Connecting = "连接中"
const val ConnectSuccess = "成功"
const val ConnectFailed = "失败"
const val Disconnect = "断开"

class BlueToothViewModel : ViewModel() {
    private val _signal = MutableLiveData<Int>()
    val signal: LiveData<Int>
        get() = _signal

    private val _att = MutableLiveData<Int>()
    val att: LiveData<Int>
        get() = _att

    private val _connResult = MutableLiveData<String>()
    val connResult: LiveData<String>
        get() = _connResult

    init {
        BlueToothHelper.callback = object : BlueToothHelperCallback {
            override fun connecting() {
                _connResult.value = Connecting
            }

            override fun connectSuccess() {
                _connResult.value = ConnectSuccess
            }

            override fun connectFailed() {
                _connResult.value = ConnectFailed
            }

            override fun disconnect() {
                _connResult.value = Disconnect
            }

            override fun onSignalReceive(sgn: Int) {
                _signal.postValue(sgn)
            }

            override fun onAttentionReceive(att: Int) {
                _att.postValue(att)
            }
        }
    }

    fun start() = BlueToothHelper.start()
    fun cancel() = BlueToothHelper.cancel()
}

